<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零件号" prop="goodsNo">
        <el-input
          v-model="queryParams.goodsNo"
          placeholder="请输入零件号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-select
          v-model="queryParams.supplier"
          clearable
          filterable
          placeholder="请选择供应商"
        >
          <el-option
            v-for="item in supplierList"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作员" prop="createBy">
        <el-select
          v-model="queryParams.createBy"
          clearable
          filterable
          placeholder="请选择操作员"
        >
          <el-option
            v-for="(item, index) in filterable"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tms:goods:add']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5"></el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          v-hasPermi="['tms:goods:import']"
        >批量导入商品
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tms:goods:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      :border="true"
      :row-class-name="tableRowClassName"
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="供应商" align="center" prop="supplier"/>
      <el-table-column label="商品中文名称" align="center" prop="goodsName"/>
      <el-table-column label="商品零件号" align="center" prop="goodsNo"/>
      <el-table-column label="体积(立方米)" align="center" prop="volume"/>
      <el-table-column label="库存数量" align="center" prop="currentStock"/>
      <el-table-column label="商品单位" align="center" prop="unit"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.goods_list_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作员"
        width="100"
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tms:goods:edit']"
          >编辑
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleInventory(scope.row)"
            v-hasPermi="['tms:goods:remove']"
          >库存流水
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tms:goods:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[10,20,30,50,500,1000]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <AddGoods :title="title" :open="open" @refresh="refresh"></AddGoods>
    <EditGoods :record="editRecord" @refresh="refresh"></EditGoods>
    <InventoryGoods :record="inventory" @refresh="refresh"></InventoryGoods>
    <FileIn :fileOpen="fileOpen" :open="fileOpen" @refresh="refresh"></FileIn>
  </div>
</template>
<script>
import {delGoods, listGoods} from "@/api/tms/goods";
import {getPeople,getSupplier} from "@/api/tms/inventory";
import AddGoods from "./components/AddGoods.vue";
import EditGoods from "./components/EditGoods.vue";
import InventoryGoods from './components/InventoryGoods.vue';
import FileIn from "./components/FileIn.vue"

export default {
  dicts: ["goods_list_status"],
  name: "Goods",
  components: {
    AddGoods,
    EditGoods,
    InventoryGoods,
    FileIn,
  },
  data() {
    return {
      // 操作员
      filterable: [],
      supplierList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      fileOpen: false,
      editRecord: {},
      inventory: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 500,
        createBy:"",
        supplier:"",
        goodsName: null,
        goodsNo: this.$route.query.goodsNo,
        volume: null,
        currentStock: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表列表 */
    getList() {
      getSupplier().then((res) => {
        this.supplierList = res.data;
      });
      getPeople().then((res) => {
        this.filterable = res.data;
      });
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentGoodsNo: null,
        categoryId: null,
        supplier: null,
        goodsName: null,
        goodsNo: null,
        unit: null,
        model: null,
        grossWeight: null,
        netWeight: null,
        volume: null,
        length: null,
        width: null,
        height: null,
        currentStock: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deleted: null,
      };
      this.resetForm("form");
    },
    getGoods(msg) {
      // console.log(msg);
      this.billListIn = msg;
      for (let i = 0; i < this.billListIn.length; i++) {
        this.billListIn[i].id = i + 1;
        if (this.billListIn[i].stock > this.billListIn[i].currentStock) {
          this.excelExport = false;
          this.color = "#FF0000";
        }
      }
    },
    /** 导入按钮操作 */
    handleImport() {
      this.fileOpen = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加商品列表";
      this.reset();
    },
    // 编辑商品列表
    handleUpdate(record) {
      this.editRecord = {
        open: true,
        form: record,
      };
    },
    // 库存流水
    handleInventory(record) {
      this.inventory = {
        open: true,
        form: record
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tms/goods/export",
        {
          ...this.queryParams,
        },
        `goods_${new Date().getTime()}.xlsx`
      );
    },
    // 表单隔行变色
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2 == 1) {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
    refresh() {
      this.open = false;
      this.fileOpen = false;
      this.editRecord.open = false;
      this.inventory.open = false;
      this.getList();
    },
    /** 删除计划 */
    handleDelete(row) {
      console.log(row);
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品零件号为"' + row.goodsNo + '"的项？')
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
  },
};
</script>
<style>
.el-table .warning-row {
  background: #f7faff;
}

.el-table .success-row {
  background: #ebf1fb;
}
</style>
